package cn.zyx.controller;

import cn.zyx.dao.ChwlinfoDao;
import cn.zyx.entity.Chwlinfo;
import cn.zyx.service.ChwlinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Chwlinfo)表控制层
 *
 * @author makejava
 * @since 2022-01-23 13:00:18
 */
@RestController
@RequestMapping("chwlinfo")
public class ChwlinfoController {
    /**
     * 服务对象
     */
    @Resource
    private ChwlinfoService chwlinfoService;
    @Resource
    private ChwlinfoDao chwlinfoDao;
    @Autowired
    private ReturnPayApi returnPayApi;




    /**
     * 根据wx手机号 查询
     */
    @RequestMapping("selphone")
    public List<Chwlinfo> selwxPhone(String wxPhone){
        return chwlinfoService.queryPhone(wxPhone);
    }
    /**
     * 根据订单编号查询
     */
    @RequestMapping("selopenid")
    public List<Chwlinfo> selorderno(String openid){
        return chwlinfoService.queryOpenid(openid);
    }

    /**
     * 添加
     * @param chwlinfo
     * @return
     */
    @RequestMapping("add")
    public Chwlinfo add(@RequestBody Chwlinfo chwlinfo) {
        VipController vipController = new VipController();
        String time = vipController.time();
//        添加下单时间
        chwlinfo.setCreatetime(time);
//        添加充值状态
        chwlinfoService.insert(chwlinfo);
        return chwlinfo;
    }

    /**
     * 订单号和微信手机号
     *
     * @param
     * @return
     */
    @RequestMapping("sel")
    public Map<String, Object> query(@RequestParam(defaultValue = "1") Integer page, Integer limit,String number,String wxPhone){

        List<Chwlinfo> chwlinfos = this.chwlinfoService.queryAllByLimit((page - 1) * limit, limit,number,wxPhone);
        for (int i = 0; i < chwlinfos.size(); i++) {
            String orderno1 = chwlinfos.get(i).getOpenid();
//            String outorderno = vip.get(i).getOutorderno();
        }
        int count = chwlinfoDao.count(number,wxPhone);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("msg","查询成功");
        map.put("data", chwlinfos);
        map.put("count", count);
        map.put("code", 0);
        return map;
    }


    /**
     * 将未充值用户进行充值
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("cupd")
    public Map<String, Object> cupd(Integer id) {
        Chwlinfo chwlinfo = new Chwlinfo();
        chwlinfo.setId(id);
        chwlinfo.setCode("0");
        VipController vipController = new VipController();
        String time = vipController.time();
        chwlinfo.setCreatetime(time);
        chwlinfoService.update(chwlinfo);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }


    /**
     * 将充值完成或者退款成功记录进行删除
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("dupd")
    public Map<String, Object> dupd(Integer id) {
        Chwlinfo chwlinfo = new Chwlinfo();
        chwlinfo.setId(id);
        chwlinfo.setIsdel(1);
        OilController oilController = new OilController();
        String time = oilController.time();
        chwlinfo.setCreatetime(time);
        chwlinfoService.update(chwlinfo);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @RequestMapping("dupds")
    public Map<String, Object> dupds(String idsStr) {
        String[] split = idsStr.split(",");
        for (int i = 0; i < split.length; i++) {
            Chwlinfo chwlinfo = new Chwlinfo();
            String s = split[i];
            chwlinfo.setId(Integer.valueOf(s));
            chwlinfo.setIsdel(1);
            chwlinfoService.update(chwlinfo);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }


    /**
     * 将充值失败用户进行退款
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("tupd")
    public Map<String, Object> tupd(Integer id, String orderNum, String money, String desc,HttpServletResponse response) {
        Chwlinfo chwlinfo1 = chwlinfoService.queryById(id);
//        int a = (int) (Float.valueOf((Float) chwlinfo1.getMoney())*100);
//        returnPayApi.refund(chwlinfo1.getNumber(),"",String.valueOf(a), response);
//        returnPayApi.refund(chwlinfo1.getNumber(), "", "1", response);
//        returnPayApi.refund(orderNum,money,desc, response);
        System.out.println(id);
        Chwlinfo chwlinfo = new Chwlinfo();
        chwlinfo.setId(id);
        chwlinfo.setCode("2");
        VipController VipController = new VipController();
        String time = VipController.time();
        chwlinfo.setCreatetime(time);
        chwlinfo.setNumber(orderNum);
        chwlinfo.setMoney(money);
        chwlinfo.setDesc(desc);
        chwlinfoService.update(chwlinfo);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }






    public String time() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        return format;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Chwlinfo selectOne(Integer id) {
        return this.chwlinfoService.queryById(id);
    }

}